﻿using Entity.Models;
using HuaRunInterface.Comm;
using HuaRunLanYanInterface.Controllers;
using HuaRunLanYanInterface.Controllers.RollbackControllers;
using Microsoft.AspNetCore.Mvc;
using RestSharp;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Utility;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HuaRunInterface.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {

        // GET: api/<ValuesController>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            string conn = Global.Configuration["ConnectionStrings:DDb"];
            using (Entity.DBContext context = Entity.BaseContext.CreateContext(conn))
            {
                string msg = "";
                List<MeterInfo> mlist = context.meterInfos.Where(p=>string.IsNullOrWhiteSpace(p.uniqueDeviceIdentifier)&&p.IsDelete==0).ToList();
                List<RTUInfo> rvList = context.rTUInfos.Where(p => string.IsNullOrWhiteSpace(p.uniqueDeviceIdentifier) && p.IsDelete == 0).ToList();
                int scrollCount = rvList.Count / 500;
                for (int i = 0; i <= scrollCount; i++)
                {
                    int skip = 500 * (i - 0);
                    List<RTURegisterModel> listrtu = new List<RTURegisterModel>();
                    listrtu = rvList.OrderByDescending(p=>p.deviceCode).Skip(skip).Take(500).SerializerObjectToString().JSONStringToList<RTURegisterModel>();

                    #region
                    //RTURegisterModel m1 = new RTURegisterModel();
                    //m1.sequence = "RTU" + DateTime.Now.ToString("yyyyMMddHHmmssff");
                    //m1.factoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //m1.domainId = "CR0071001601";
                    //m1.deviceCode = (28003230600023 + 1) + "";
                    //m1.deviceType = "15";
                    //m1.protocolTypeCode = "99999";
                    //m1.commMode = "5";
                    //m1.nbImei = (28003230600023 + 1) + "";
                    //m1.nbImsi = (28003230600023 + 1) + "";
                    //listrtu.Add(m1);
                    //RTURegisterModel m2 = new RTURegisterModel();
                    //m2.sequence = "RTU" + DateTime.Now.ToString("yyyyMMddHHmmssff");
                    //m2.factoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //m2.domainId = "CR0071001601";
                    //m2.deviceCode = (28003230600023 + 2) + "";
                    //m2.deviceType = "15";
                    //m2.protocolTypeCode = "99999";
                    //m2.commMode = "5";
                    //m2.nbImei = (28003230600023 + 2) + "";
                    //m2.nbImsi = (28003230600023 + 2) + "";
                    //listrtu.Add(m2);
                    //RTURegisterModel m3 = new RTURegisterModel();
                    //m3.sequence = "RTU" + DateTime.Now.ToString("yyyyMMddHHmmssff");
                    //m3.factoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //m3.domainId = "CR0071001601";
                    //m3.deviceCode = (28003230600023 + 3) + "";
                    //m3.deviceType = "15";
                    //m3.protocolTypeCode = "99999";
                    //m3.commMode = "5";
                    //m3.nbImei = (28003230600023 + 3) + "";
                    //m3.nbImsi = (28003230600023 + 3) + "";
                    //listrtu.Add(m3);
                    //RTURegisterModel m4 = new RTURegisterModel();
                    //m4.sequence = "RTU" + DateTime.Now.ToString("yyyyMMddHHmmssff");
                    //m4.factoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //m4.domainId = "CR0071001601";
                    //m4.deviceCode = (28003230600023 + 4) + "";
                    //m4.deviceType = "15";
                    //m4.protocolTypeCode = "99999";
                    //m4.commMode = "5";
                    //m4.nbImei = (28003230600023 + 4) + "";
                    //m4.nbImsi = (28003230600023 + 4) + "";
                    //listrtu.Add(m4);
                    //RTURegisterModel m5 = new RTURegisterModel();
                    //m5.sequence = "RTU" + DateTime.Now.ToString("yyyyMMddHHmmssff");
                    //m5.factoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //m5.domainId = "CR0071001601";
                    //m5.deviceCode = (28003230600023 + 5) + "";
                    //m5.deviceType = "15";
                    //m5.protocolTypeCode = "99999";
                    //m5.commMode = "5";
                    //m5.nbImei = (28003230600023 + 5) + "";
                    //m5.nbImsi = (28003230600023 + 5) + "";
                    //listrtu.Add(m5);
                    //RTURegisterModel m6 = new RTURegisterModel();
                    //m6.sequence = "RTU" + DateTime.Now.ToString("yyyyMMddHHmmssff");
                    //m6.factoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //m6.domainId = "CR0071001601";
                    //m6.deviceCode = (28003230600023 + 6) + "";
                    //m6.deviceType = "15";
                    //m6.protocolTypeCode = "99999";
                    //m6.commMode = "5";
                    //m6.nbImei = (28003230600023 + 6) + "";
                    //m6.nbImsi = (28003230600023 + 6) + "";
                    //listrtu.Add(m6);
                    //RTURegisterModel m7 = new RTURegisterModel();
                    //m7.sequence = "RTU" + DateTime.Now.ToString("yyyyMMddHHmmssff");
                    //m7.factoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //m7.domainId = "CR0071001601";
                    //m7.deviceCode = (28003230600023 + 7) + "";
                    //m7.deviceType = "15";
                    //m7.protocolTypeCode = "99999";
                    //m7.commMode = "5";
                    //m7.nbImei = (28003230600023 + 7) + "";
                    //m7.nbImsi = (28003230600023 + 7) + "";
                    //listrtu.Add(m7);
                    //RTURegisterModel m8 = new RTURegisterModel();
                    //m8.sequence = "RTU" + DateTime.Now.ToString("yyyyMMddHHmmssff");
                    //m8.factoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //m8.domainId = "CR0071001601";
                    //m8.deviceCode = (28003230600023 + 8) + "";
                    //m8.deviceType = "15";
                    //m8.protocolTypeCode = "99999";
                    //m8.commMode = "5";
                    //m8.nbImei = (28003230600023 + 8) + "";
                    //m8.nbImsi = (28003230600023 + 8) + "";
                    //listrtu.Add(m8);
                    //RTURegisterModel m9 = new RTURegisterModel();
                    //m9.sequence = "RTU" + DateTime.Now.ToString("yyyyMMddHHmmssff");
                    //m9.factoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //m9.domainId = "CR0071001601";
                    //m9.deviceCode = (28003230600023 + 9) + "";
                    //m9.deviceType = "15";
                    //m9.protocolTypeCode = "99999";
                    //m9.commMode = "5";
                    //m9.nbImei = (28003230600023 + 9) + "";
                    //m9.nbImsi = (28003230600023 + 9) + "";
                    //listrtu.Add(m9);
                    //RTURegisterModel m10 = new RTURegisterModel();
                    //m10.sequence = "RTU" + DateTime.Now.ToString("yyyyMMddHHmmssff");
                    //m10.factoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //m10.domainId = "CR0071001601";
                    //m10.deviceCode = (28003230600023 + 10) + "";
                    //m10.deviceType = "15";
                    //m10.protocolTypeCode = "99999";
                    //m10.commMode = "5";
                    //m10.nbImei = (28003230600023 + 10) + "";
                    //m10.nbImsi = (28003230600023 + 10) + "";
                    //listrtu.Add(m10);
                    #endregion
                    Global.RTURegister(listrtu, ref msg);
                    WriteLog.WriteLogFile($"skip : {skip},msg :{msg},devicecodes :{listrtu.Select(p=>p.deviceCode).SerializerObjectToString()}");
                }
                scrollCount = mlist.Count / 500;
                for (int i = 0; i <= scrollCount; i++)
                {
                    int skip = 500 * (i - 0);
                    List<MeterRegisterModel> listmeter = new List<MeterRegisterModel>();
                    listmeter = mlist.OrderByDescending(p=>p.meterCode).Skip(skip).Take(500).SerializerObjectToString().JSONStringToList<MeterRegisterModel>();
                    #region
                    //MeterRegisterModel mm1 = new MeterRegisterModel();
                    //mm1.sequence = "Meter" + DateTime.Now.ToString("yyyyMMddHHmmssff");
                    //mm1.meterFactoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //mm1.mrFactoryCode = "2000";
                    //mm1.protocolTypeCode = "9999";
                    //if (mm1.protocolTypeCode != "10100")
                    //{
                    //    mm1.mrFactoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //}
                    //mm1.domainId = "CR0071001601";
                    //mm1.meterCode = (00002021033131 + 3) + "";
                    //mm1.conMeterCode= (00002021033131 + 3) + "";
                    //mm1.modelCode = "G1.6";
                    //mm1.meterTypeCode = "2201";
                    //mm1.baseNo = 0;
                    //mm1.installDate = DateTime.Now.ToString();
                    //mm1.inDirection = "01";
                    //mm1.commMode= "5";
                    //mm1.installPostion = "ceshi";
                    //mm1.longitude = "116.5";
                    //mm1.latitude = "25.6";
                    //mm1.valveStatus = 1;
                    //mm1.meterCategory = "34";
                    //mm1.calcTypeCode = "2";
                    //listmeter.Add(mm1);

                    //MeterRegisterModel mm2 = new MeterRegisterModel();
                    //mm2.sequence = "Meter" + DateTime.Now.ToString("yyyyMMddHHmmssff");
                    //mm2.meterFactoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //mm2.mrFactoryCode = "2000";
                    //mm2.protocolTypeCode = "9999";
                    //if (mm2.protocolTypeCode != "10100")
                    //{
                    //    mm2.mrFactoryCode = Global.Configuration["AppSettings:FactoryCode"];
                    //}
                    //mm2.domainId = "CR0071001601";
                    //mm2.meterCode = (00002021033131 + 4) + "";
                    //mm2.conMeterCode = (00002021033131 + 4) + "";
                    //mm2.modelCode = "G1.6";
                    //mm2.meterTypeCode = "2201";
                    //mm2.baseNo = 0;
                    //mm2.installDate = DateTime.Now.ToString();
                    //mm2.inDirection = "01";
                    //mm2.commMode = "5";
                    //mm2.installPostion = "ceshi";
                    //mm2.longitude = "116.5";
                    //mm2.latitude = "25.6";
                    //mm2.valveStatus = 1;
                    //mm2.meterCategory = "34";
                    //mm2.calcTypeCode = "2";
                    //listmeter.Add(mm2);
                    #endregion
                    Global.MeterRegister(listmeter, ref msg);
                    WriteLog.WriteLogFile($"skip : {skip},msg :{msg},meterCodes :{listmeter.Select(p => p.meterCode).SerializerObjectToString()}");
                }
            }
            //string conn = Global.Configuration["ConnectionStrings:DDb"];
            //Entity.DBContext context = Entity.BaseContext.CreateContext(conn);

            //string apiInterface = "api/Conf/CtrlData/Add";
            //string e = "";
            //bool result = Global.CommonSend(apiInterface, "", ref e);

            //using (Entity.DBContext context = Entity.BaseContext.CreateContext(conn))
            //{
            //    List<RechargeInfo> recharges = context.rechargeInfos.Where(p => p.deviceCode == "28003230600023").OrderByDescending(p => p.RechargeTime).ToList();
            //    if (recharges.Count == 0 || recharges[0].recentPaymentTime != "1700623635000")
            //    {

            //    }
            //}
            string sss = Global.Token;
            DateTime d1 = "1700210669000".ConvertStringToDateTime();
            //ReqRemainMoneyModel rmm = new ReqRemainMoneyModel();
            //rmm.cmdParam = new CmdParamModel();
            //rmm.cmdParam.settlementNum = 1;
            //rmm.cmdParam.settlementTime = DateTime.Now;
            //string s = rmm.SerializerObjectToString();

            List<KeyValueModel> list = Global.ProtocolTypes;
            //var options = new RestClientOptions("https://iotee-uat.crcgas.com")
            //{
            //    MaxTimeout = -1,
            //};
            //var client = new RestClient(options);
            //var request = new RestRequest("/eciot-openapi/area/getAllCompanyForAreaConfig", Method.Post);
            //request.AddHeader("Authorization", "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImJhZWwta2V5LWlkIn0.eyJzdWIiOiJJT1RVQVRfR1ouY3JjZ2FzIiwidXNlcl9yZXBvc2l0b3J5IjoiZGVmYXVsdCIsImlzcyI6ImNyY2dhcy5jb20iLCJwcm9kX2NvZGUiOiJJT1RVQVRfR1oiLCJjbGllbnRfaWQiOiJJT1RVQVRfR1ouY3JjZ2FzIiwibG9uZ190b2tlbl9mbGFnIjoiWSIsImtleV92ZXJzaW9uIjoiMDAxIiwidXNlcl9pZCI6IklPVFVBVF9HWi5jcmNnYXMiLCJzY29wZSI6WyJub25lIl0sIm5pY2tfbmFtZSI6IklPVFVBVF9HWi5jcmNnYXMiLCJleHAiOjE2OTY5Mjc0NDEsInRva2VuX3NvdXJjZSI6IkFQUCIsImp0aSI6IjhjOTYxZGVhLTA4ZjgtNGIxYy04MmNkLWYyNmU0MmNkNzM5MiIsInRlbmFudF9jb2RlIjoiY3JjZ2FzIn0.GQPqu36hOSrsZtYhe9E_MAMBGMo1OKEuwU1jmnvNSUPAF4hWhwUu8qWH9LngFtR-ydWdxsKowkMsFJwm3ahTPKzuDr9P-JHQWIsQqYF_XzBMfJCYf6lBxwAeWI4viaYGI4TK7gdePVuNnkNHsEmv9ceKqHx0A9STaQslLqd6_rtnS0z4dU5yqVivT-lV49IS1DMxJJjh3vt0IFYty_hbQAJo2oynSMBnBkswOpekodl5fb5Rg_7fxbqIXkpap4umOxZSNFkIJV7mqS7j69OBliUTkEuFOjZUfDsxrvbHb2H_d7aFGZTL9dnIjDN0V9dbT7PdNbHx_uYBUWHc7YkfPg");
            //request.AddHeader("Content-Type", "application/json");
            //var body = "{}";
            //request.AddStringBody(body, DataFormat.Json);
            //RestResponse response = client.Execute(request);
            //string ssss = response.Content;

            //HttpService.Post("{}", "https://iotee-uat.crcgas.com/eciot-openapi/area/getAllCompanyForAreaConfig", 50, "application/json;charset=utf-8;", "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImJhZWwta2V5LWlkIn0.eyJzdWIiOiJJT1RVQVRfR1ouY3JjZ2FzIiwidXNlcl9yZXBvc2l0b3J5IjoiZGVmYXVsdCIsImlzcyI6ImNyY2dhcy5jb20iLCJwcm9kX2NvZGUiOiJJT1RVQVRfR1oiLCJjbGllbnRfaWQiOiJJT1RVQVRfR1ouY3JjZ2FzIiwibG9uZ190b2tlbl9mbGFnIjoiWSIsImtleV92ZXJzaW9uIjoiMDAxIiwidXNlcl9pZCI6IklPVFVBVF9HWi5jcmNnYXMiLCJzY29wZSI6WyJub25lIl0sIm5pY2tfbmFtZSI6IklPVFVBVF9HWi5jcmNnYXMiLCJleHAiOjE2OTY5Mjc0NDEsInRva2VuX3NvdXJjZSI6IkFQUCIsImp0aSI6IjhjOTYxZGVhLTA4ZjgtNGIxYy04MmNkLWYyNmU0MmNkNzM5MiIsInRlbmFudF9jb2RlIjoiY3JjZ2FzIn0.GQPqu36hOSrsZtYhe9E_MAMBGMo1OKEuwU1jmnvNSUPAF4hWhwUu8qWH9LngFtR-ydWdxsKowkMsFJwm3ahTPKzuDr9P-JHQWIsQqYF_XzBMfJCYf6lBxwAeWI4viaYGI4TK7gdePVuNnkNHsEmv9ceKqHx0A9STaQslLqd6_rtnS0z4dU5yqVivT-lV49IS1DMxJJjh3vt0IFYty_hbQAJo2oynSMBnBkswOpekodl5fb5Rg_7fxbqIXkpap4umOxZSNFkIJV7mqS7j69OBliUTkEuFOjZUfDsxrvbHb2H_d7aFGZTL9dnIjDN0V9dbT7PdNbHx_uYBUWHc7YkfPg");


            //object s = Global.GetAccessToken();
            //bool b = Global.GetCompanyInfo();
            //RTURegisterModel m = new RTURegisterModel();
            //m.sequence = "123";
            //m.deviceCode = "1456";
            //m.domainId = "CR0071001601";
            //m.factoryCode = "0025";
            //m.deviceType = "15";
            //m.protocolTypeCode = "99999";
            //m.commMode = "123";
            //bool r = Global.RTURegister(m);

            return new string[] { "value1", "value2" };
        }

        // GET api/<ValuesController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<ValuesController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/<ValuesController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<ValuesController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
